<?php
namespace app\api\controller;
// namespace app\zw_admin\controller;
use \think\Controller;
use think\Db;
// use think\Controller;
use think\Session;
use think\Request;
use Endroid\QrCode\Tests;
use qrcode;
use alisms\SmsDemo;
class LoginController extends Controller
{


    public function index()
    {
        echo '111';
        exit;
    }

    /**
     * 注册短信验证
     * @return [type] [description]
     */
    public function verification()
    {
        include_once ROOT_PATH.'extend/aliyun-dysms-php-sdk/api_demo/smsdemo.php';
        // dump(input('param.'));
        $phone = input('param.phone');
        if (empty($phone)) {
            echo json_encode(array("status"=>6,'message'=>'手机号码不能为空',"data"=>[]));
            exit;
        }
        $TemplateCode = 'SMS_127153628';
        $Verification_one = rand(1000,9999);
        $Verification_one = (string)$Verification_one;
        $phone_list = db('member')->where(['phone'=>$phone])->find();
        //手机号码验证
        if (!empty($phone_list)) {
            echo json_encode(array("status"=>3,'message'=>'手机号码已存在',"data"=>[]));
            exit;
        }

        $vas_phone = Db::name('verification')->where(['phone'=>$phone])->find();
        // dump($vas_phone);
        // exit;
        if ($vas_phone['num'] >= 5) {
            echo json_encode(array("status"=>4,'message'=>'请不要重复申请验证码',"data"=>[]));
            exit;
        }
        if (empty($vas_phone)) {
            // $list['ip']    = $_SERVER["REMOTE_ADDR"];
            $list['phone'] = $phone;
            $list['vas']   = $Verification_one;
            $list['time']  = date('Y-m-d H:i');
            $list['num']   = '1';
            // $insert_array[] = $list;
            // dump($insert_array);
            // exit;
            db('verification')->insert($list);
            // echo '111';
            // exit;
        }else if($vas_phone['num'] < '5'){
                $list['vas']    = $Verification_one;
                $list['time']   = date('Y-m-d H:i');
                $list['num']  = $vas_phone['num'] + 1;
                db('verification')->where(array('phone'=>$phone))->update($list);
                
        }else{
            echo json_encode(array("status"=>5,'message'=>'系统错误',"data"=>[]));
            exit;
        }

        $Verification = $Verification_one;
        $response = SmsDemo::sendSms($phone,$TemplateCode,$Verification);
        if ($response) {
            echo json_encode(array("status"=>1,'message'=>'短信验证码发送成功',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'短信验证码发送失败',"data"=>[]));
            exit;
        }
        exit;
    }




    /**
     * 登入短信验证
     * @return [type] [description]
     */
    public function verification_login()
    {
        
        include_once ROOT_PATH.'extend/aliyun-dysms-php-sdk/api_demo/smsdemo.php';
        // dump(input('param.'));
        $phone = input('param.phone');
        if (empty($phone)) {
            echo json_encode(array("status"=>6,'message'=>'手机号码不能为空',"data"=>[]));
            exit;
        }
        $member_list = db('member')->where(['phone'=>$phone])->find();
        if (empty($member_list)) {
        	echo json_encode(array("status"=>1,'message'=>'手机号码不存在',"data"=>[]));
            exit;
        }
        $TemplateCode = 'SMS_141597244';
        $Verification_one = rand(1000,9999);
        $Verification_one = (string)$Verification_one;
        $vas_phone = Db::name('verification_login')->where(['phone'=>$phone])->find();
        if (empty($vas_phone)) {
            // $list['ip']    = $_SERVER["REMOTE_ADDR"];
            $list['phone'] = $phone;
            $list['vas']   = $Verification_one;
            $list['time']  = date('Y-m-d H:i');
            db('verification_login')->insert($list);
        }else{
            $list['vas']    = $Verification_one;
            $list['time']   = date('Y-m-d H:i');
            db('verification_login')->where(array('phone'=>$phone))->update($list);    
        }

        $Verification = $Verification_one;
        $response = SmsDemo::sendSms($phone,$TemplateCode,$Verification);
        if ($response) {
            echo json_encode(array("status"=>1,'message'=>'短信验证码发送成功',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'短信验证码发送失败',"data"=>[]));
            exit;
        }
        exit;
    }





    /**
     * 用户注册接口
     * @return [type] [description]
     */
    public function register()
    {
        $vas = input('param.code');
        $phone = input('param.phone');
        $pwd = input('param.pwd');
        $pwdto = input('param.pwdto');
        if ($pwd != $pwdto) {
            echo json_encode(array("status"=>4,'message'=>'两次密码不一样!',"data"=>[]));
            exit;
        }
        $vas_list = Db::name('verification')->where(['phone'=>$phone])->find();
        // dump($vas_list);
        if ($vas != $vas_list['vas']) {
            //如果验证码不正确 ，返回false;
            echo json_encode(array("status"=>3,'message'=>'短信验证码不正确',"data"=>[]));
            exit;
        }

        $list['pwd'] = md5(md5($pwd));
        $list['phone'] = $phone;
        $list['time'] = date('Y-m-d H:i');
        $uid = Db::name('member')->insertGetId($list);
        // echo $uid;
        // exit;
        $auuto['uid'] = $uid;
        $auuto['user_img'] = 'http://www.mochoushop.com/uploads/20180930/c1557148550b36124a2d20b3ab1dd305.jpg';
        $auuto['date'] = date('Y-m-d');
        $auuto['phone'] = $phone;
       
        if (db('account')->insert($auuto) > 0) {
            echo json_encode(array("status"=>1,'message'=>'注册成功!',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'注册失败!',"data"=>[]));
            exit;
        }
        // if (db('member')->insert($list)) {
            
        // }else{
            
        // }
    }


    /**
     * 登入操作
     * @return [type] [description]
     */
    public function login()
    {
        $phone = input('param.phone');

        $type = input('param.type');
        if (empty($phone)) {
        	echo json_encode(array("status"=>4,'message'=>'账号不能为空!',"data"=>['uid'=>0]));
            exit;
        }
        switch ($type) {
            case '1':  //:短信验证码登入
                $vas = input('param.code');
                $phone = input('param.phone');
                $vas_list = Db::name('verification_login')->where(['phone'=>$phone])->find();
                // dump($vas_list);
                if ($vas != $vas_list['vas']) {
                    //如果验证码不正确 ，返回false;
                    echo json_encode(array("status"=>3,'message'=>'短信验证码不正确',"data"=>[]));
                    exit;
                }
                $member_list = Db::name('member')->where(['phone'=>$phone])->find();
                if (!empty($member_list)) {
                    echo json_encode(array("status"=>1,'message'=>'登入成功!',"data"=>['uid'=>$member_list['uid']]));
                    exit;
                }else{
                    echo json_encode(array("status"=>2,'message'=>'您还没有注册!',"data"=>['uid'=>0]));
                    exit;
                }
                break;


            case '2':  //2:账号密码登入
            	$pwd_to = input('param.pwd');
        		$pwd = md5(md5(input('param.pwd')));
            	if (empty($pwd_to)) {
		        	echo json_encode(array("status"=>4,'message'=>'密码输入不正确!',"data"=>['uid'=>0]));
		            exit;
		        }
                $member_list = Db::name('member')->where(['phone'=>$phone,'pwd'=>$pwd])->find();
                if (!empty($member_list)) {
                    echo json_encode(array("status"=>1,'message'=>'登入成功!',"data"=>['uid'=>$member_list['uid']]));
                    exit;
                }else{
                    echo json_encode(array("status"=>2,'message'=>'账号或密码错误!',"data"=>['uid'=>0]));
                    exit;
                }

                
                break;
        }
    }




    










    
}
